package com._51cto.healthy.controller;

import com._51cto.healthy.content.P;
import com._51cto.healthy.content.R;
import com._51cto.healthy.pojo.Member;
import com._51cto.healthy.service.BackUpsService;
import com._51cto.healthy.service.MemberService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {

    @DubboReference
    private MemberService memberService;
    @DubboReference
    private BackUpsService backUpsService;

    @GetMapping("/page/{pageSize}/{pageNum}")
    public R queryMember(@RequestParam(value = "query",required = false) String query,
                      @PathVariable("pageNum") Integer pageNum,
                      @PathVariable("pageSize") Integer pageSize) {
        P<Member> memberP = null;
        boolean b = false;
        while(!b){
            try{
                memberP = memberService.queryMember(query, pageNum, pageSize);
                b = true;
            }catch(RuntimeException e ){
                b = false;
            }
        }
        return R.success(memberP);
    }

    @DeleteMapping("/{id}")
    public R deleteByPk(@PathVariable Integer id){
        boolean a = false;
        while (!a){
            try {
                backUpsService.insertById(id);
                a = true;
            }catch (RuntimeException e){
                a = false;
            }
        }
       return R.success("删除成功");
    }

    @PostMapping("")
    public R insertMember(@RequestBody Member member){
        boolean b = false;
        while(!b){
            try{
                memberService.insert(member);
                b = true;
            }catch (RuntimeException e){
                b = false;
            }
        }
        return R.success("添加成功");
    }

    @PutMapping("")
    public R updateMember(@RequestBody Member member){
        boolean c = false;
        while(!c){
            try{
                memberService.update(member);
                c = true;
            }catch (RuntimeException e){
                c = false;
            }
        }
        return R.success("更新成功");
    }
}
